<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Start of file recap.php */
/* Location: ./cashflow/controllers/recap.php */
class Recap extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('accounts'); /** load table rekening **/ 
        $this->load->model('cheques'); /** load table cheque **/ 
        $this->load->model('giroes'); /** load table giro **/ 
        $this->load->model('transfers'); /** load table transfer **/
        if(!$this->session->userdata('username')) /** restrict area if not acces to login page**/
			redirect('/',301); 
    }
    
    function cheque(){
        $this->ws->set('title',lang('recap_cheque'));/** title page */
		$this->ws->set('username',$this->session->userdata('username')); /** username session */
		$this->ws->set('department',$this->session->userdata('dep')); /** departement session */
        $this->ws->set('accounts',$this->accounts->getAccountData()); /** table rekening */
        /** all session for post data**/
        if($this->session->userdata('cheque_day')):
            $day  =   $this->session->userdata('cheque_day');
            $day2 =   $this->session->userdata('cheque_day2');
            $acc  =   $this->session->userdata('cheque_acc');
        else:
            $day  =   '-';    
            $day2 =   '-'; 
            $acc  =   '-';      
        endif;
        $this->ws->set('cheques',$this->cheques->getAllCheque($day,$day2,$acc)); /** table cheque & pdo_cheque */
        $this->ws->set_theme('smooth'); /** set theme to smooth*/
		$this->ws->view('cheque/index'); /** view layout to views/cheque/index*/
    }
    
    function getCheque(){
        /** post to variabel session **/
        $value  =   array('cheque_day'  =>  $this->input->post('year').'-'.$this->input->post('month').'-'.$this->input->post('day'),
                          'cheque_day2' =>  $this->input->post('year2').'-'.$this->input->post('month2').'-'.$this->input->post('day2'),
                          'cheque_acc'  =>  $this->input->post('account'));
        $this->session->set_userdata($value); /** insert session array to userdata **/
        redirect('cashflow/recap/cheque',301); /** redirect to function cheque **/
    }
    
    function getChequeReset(){
        /** post to variabel session **/
        $value  =   array('cheque_day'  =>  'cheque_day',
                          'cheque_day2' =>  'cheque_day2',
                          'cheque_acc'  =>  'cheque_acc');
        $this->session->unset_userdata($value); /** reset session array to userdata **/
        redirect('cashflow/recap/cheque',301); /** redirect to function cheque **/
    }
    
    function getChequeExport(){
        /** Provider Variabel **/
        $day01   =   $this->session->userdata('cheque_day');
        $day02   =   $this->session->userdata('cheque_day2');
        $acc     =   $this->session->userdata('cheque_acc');
        /** if export via excel **/    
		if($this->input->post('export')=='excel')
            redirect('cashflow/recap/getChequeExcel/'); /** call function getChequeExcel **/
        elseif($this->input->post('export')=='pdf')    
            redirect('cashflow/recap/getChequePdf/'); /** call function getChequePdf **/
        elseif($this->input->post('export')=='email')    
            redirect('cashflow/recap/getChequeEmail/'); /** call function getChequeEmail **/
		else
			echo 'Error redirecting'; /** call Print Error if not selected **/
    }
    
    function getChequePdf(){
        /** variabel to provider **/    
        $day01  =   $this->session->userdata('cheque_day');
        $day02  =   $this->session->userdata('cheque_day2');
        $acc    =   $this->session->userdata('cheque_acc');
		$this->ws->set('cheques',$this->cheques->getAllCheque($day01,$day02,$acc)); /** table cheque & pdo_cheque **/
        $this->ws->set_theme('smooth-report'); /** set theme to smooth report **/
		$data = $this->ws->view('cheque/pdf','',TRUE); /** view layout to views/cheque/pdf */
        pdf_create($data,'Rekap-Cek'); /** call a function to create a pdf **/
    }
   
   function getChequeExcel(){
        /** variabel to provider **/    
        $day01  =   $this->session->userdata('cheque_day');
        $day02  =   $this->session->userdata('cheque_day2');
        $acc    =   $this->session->userdata('cheque_acc');
        
       	$excel  =   $this->cheques->getExcel($day01,$day02,$acc); /** file excell */
        $value  =   file_get_contents($excel); /** Read the file's contents **/
        $name   =   'rekap-cheque.xls'; /** file name content **/
        force_download($name,$value); /** download the file **/
   }
   
   function getChequeEmail(){
        /** variabel to provider **/    
        $day01  =   $this->session->userdata('cheque_day');
        $day02  =   $this->session->userdata('cheque_day2');
        $acc    =   $this->session->userdata('cheque_acc');
        
        /** email configuration **/
        $mail_config    =   $this->config->item('mail');
        $smtp_host      =   $mail_config['smtp_host'];
        $smtp_port      =   $mail_config['smtp_port'];
        $from_mail      =   $mail_config['from_mail'];
        $from_name      =   $mail_config['from_name'];
        
        /** php ini set email **/
        ini_set("SMTP",$smtp_host);
        ini_set("smtp_port",$smtp_port);
        ini_set('sendmail_from',$from_mail);
        /** Properties of export **/
        $subject        = 'Rekap Cheque No Rekening.'.$acc." Periode ". indonesian_date($day01) .' s/d '.indonesian_date($day02);
        $contents       = 'Dilampirkan File Cheque No Rekening '.$acc." Periode ". indonesian_date($day01) .' s/d '.indonesian_date($day02);
        $this->email->initialize($mail_config);
        $this->email->from($from_mail,$from_name);
        $email  =   'hendarsyahss@gmail.com';
        $this->email->to($email); /** email to */
        $this->email->subject($subject); /** email subject */
        $this->email->message($contents); /** email content */
        $excel          =   $this->cheques->getExcel($day01,$day02,$acc); /** get file  from excel download */
        $this->email->attach($excel);  /** email attachement */
        $d=$this->email->send(); /** email send */
        if(!$d):
            $this->session->set_flashdata('error','error');
        else:
			$this->session->set_flashdata('success','success');
        endif;
        redirect('cashflow/recap/cheque',301);
    }
    
    function giro(){
        $this->ws->set('title',lang('recap_giro'));/** title page */
		$this->ws->set('username',$this->session->userdata('username')); /** username session */
		$this->ws->set('department',$this->session->userdata('dep')); /** departement session */
        $this->ws->set('accounts',$this->accounts->getAccountData()); /** table rekening */
        /** all session for post data**/
        if($this->session->userdata('giro_day')):
            $day  =   $this->session->userdata('giro_day');
            $day2 =   $this->session->userdata('giro_day2');
            $acc  =   $this->session->userdata('giro_acc');
        else:
            $day  =   '-';    
            $day2 =   '-'; 
            $acc  =   '-';      
        endif;
        $this->ws->set('giroes',$this->giroes->getAllGiroes($day,$day2,$acc)); /** table giro & pdo_giro */
        $this->ws->set_theme('smooth'); /** set theme to smooth*/
		$this->ws->view('giro/index'); /** view layout to views/giro/index*/
    } 
    
    function getGiro(){
        /** post to variabel session **/
        $value  =   array('giro_day'  =>  $this->input->post('year').'-'.$this->input->post('month').'-'.$this->input->post('day'),
                          'giro_day2' =>  $this->input->post('year2').'-'.$this->input->post('month2').'-'.$this->input->post('day2'),
                          'giro_acc'  =>  $this->input->post('account'));
        $this->session->set_userdata($value); /** insert session array to userdata **/
        redirect('cashflow/recap/giro',301); /** redirect to function giro **/
    }
    
    function getGiroExport(){
        /** Provider Variabel **/
        $day01   =   $this->session->userdata('giro_day');
        $day02   =   $this->session->userdata('giro_day2');
        $acc     =   $this->session->userdata('giro_acc');
        /** if export via excel **/    
		if($this->input->post('export')=='excel')
            redirect('cashflow/recap/getGiroExcel/'); /** call function getGiroExcel **/
        elseif($this->input->post('export')=='pdf')    
            redirect('cashflow/recap/getGiroPdf/'); /** call function getGiroPdf **/
        elseif($this->input->post('export')=='email')    
            redirect('cashflow/recap/getGiroEmail/'); /** call function getGiroEmail **/
		else
			echo 'Error redirecting'; /** call Print Error if not selected **/
    }
    
    function getGiroExcel(){
        /** variabel to post **/    
        $day01  =   $this->session->userdata('giro_day');
        $day02  =   $this->session->userdata('giro_day2');
        $acc    =   $this->session->userdata('giro_acc');
        
       	$excel  =   $this->giroes->getExcel($day01,$day02,$acc); /** file excell */
        $value  =   file_get_contents($excel); /** Read the file's contents **/
        $name   =   'rekap-giro.xls'; /** file name content **/
        force_download($name,$value); /** download the file **/
    }
   
    function getGiroPdf(){
        /** variabel to post **/    
        $day01  =   $this->session->userdata('giro_day');
        $day02  =   $this->session->userdata('giro_day2');
        $acc    =   $this->session->userdata('giro_acc');
		$this->ws->set('giroes',$this->giroes->getAllGiroes($day01,$day02,$acc)); /** table giro & pdo_giro **/
        $this->ws->set_theme('smooth-report'); /** set theme to smooth report **/
		$data = $this->ws->view('giro/pdf','',TRUE); /** view layout to views/giro/pdf */
        pdf_create($data,'Rekap-Giro'); /** call a function to create a pdf **/
    }
   
    function getGiroEmail(){
        /** variabel to post **/    
        $day01  =   $this->session->userdata('giro_day');
        $day02  =   $this->session->userdata('giro_day2');
        $acc    =   $this->session->userdata('giro_acc');
        
        /** email configuration **/
        $mail_config    =   $this->config->item('mail');
        $smtp_host      =   $mail_config['smtp_host'];
        $smtp_port      =   $mail_config['smtp_port'];
        $from_mail      =   $mail_config['from_mail'];
        $from_name      =   $mail_config['from_name'];
        
        /** php ini set email **/
        ini_set("SMTP",$smtp_host);
        ini_set("smtp_port",$smtp_port);
        ini_set('sendmail_from',$from_mail);
        /** Properties of export **/
        $subject        = 'Rekap Giro No Rekening.'.$acc." Periode ". indonesian_date($day01) .' s/d '.indonesian_date($day02);
        $contents       = 'Dilampirkan File Giro No Rekening '.$acc." Periode ". indonesian_date($day01) .' s/d '.indonesian_date($day02);
        $this->email->initialize($mail_config);
        $this->email->from($from_mail,$from_name);
        $email  =   'hendarsyahss@gmail.com';
        $this->email->to($email); /** email to */
        $this->email->subject($subject); /** email subject */
        $this->email->message($contents); /** email content */
        $excel  =   $this->giroes->getExcel($day01,$day02,$acc); /** get file  from excel download */
        $this->email->attach($excel);  /** email attachement */
        $d=$this->email->send(); /** email send */
        if(!$d):
            $this->session->set_flashdata('error','error');
        else:
			$this->session->set_flashdata('success','success');
        endif;
        redirect('cashflow/recap/giro',301);
    }
    
    function getGiroReset(){
        /** post to variabel session **/
        $value  =   array('giro_day'  =>  'giro_day',
                          'giro_day2' =>  'giro_day2',
                          'giro_acc'  =>  'giro_acc');
        $this->session->unset_userdata($value); /** reset session array to userdata **/
        redirect('cashflow/recap/giro',301); /** redirect to function giro **/
    }
    
    function transfer(){
        $this->ws->set('title',lang('recap_transfer'));/** title page */
		$this->ws->set('username',$this->session->userdata('username')); /** username session */
		$this->ws->set('department',$this->session->userdata('dep')); /** departement session */
        $this->ws->set('accounts',$this->accounts->getAccountData()); /** table rekening */
        /** all session for post data**/
        if($this->session->userdata('trf_day')):
            $day  =   $this->session->userdata('trf_day');
            $day2 =   $this->session->userdata('trf_day2');
            $acc  =   $this->session->userdata('trf_acc');
        else:
            $day  =   '-';    
            $day2 =   '-'; 
            $acc  =   '-';      
        endif;
        $this->ws->set('transfers',$this->transfers->getAllTransfer($day,$day2,$acc)); /** table transfer & pdo_transfer */
        $this->ws->set_theme('smooth'); /** set theme to smooth*/
		$this->ws->view('transfer/index'); /** view layout to views/transfer/index*/
    }
    
    function getTransfer(){
        /** post to variabel session **/
        $value  =   array('trf_day'  =>  $this->input->post('year').'-'.$this->input->post('month').'-'.$this->input->post('day'),
                          'trf_day2' =>  $this->input->post('year2').'-'.$this->input->post('month2').'-'.$this->input->post('day2'),
                          'trf_acc'  =>  $this->input->post('account'));
        $this->session->set_userdata($value); /** insert session array to userdata **/
        redirect('cashflow/recap/transfer',301); /** redirect to function transfer **/
    }
    
    function getTransferExport(){
        /** Provider Variabel **/
        $day01   =   $this->session->userdata('trf_day');
        $day02   =   $this->session->userdata('trf_day2');
        $acc     =   $this->session->userdata('trf_acc');
        /** if export via excel **/    
		if($this->input->post('export')=='excel')
            redirect('cashflow/recap/getTransferExcel/'); /** call function getTransferExcel **/
        elseif($this->input->post('export')=='pdf')    
            redirect('cashflow/recap/getTransferPdf/'); /** call function getTransferPdf **/
        elseif($this->input->post('export')=='email')    
            redirect('cashflow/recap/getTransferEmail/'); /** call function getTransferEmail **/
		else
			echo 'Error redirecting'; /** call Print Error if not selected **/
    }
    
    function getTransferExcel(){
        /** variabel to post **/    
        $day01  =   $this->session->userdata('trf_day');
        $day02  =   $this->session->userdata('trf_day2');
        $acc    =   $this->session->userdata('trf_acc');
        
       	$excel  =   $this->transfers->getExcel($day01,$day02,$acc); /** file excell */
        $value  =   file_get_contents($excel); /** Read the file's contents **/
        $name   =   'rekap-transfer.xls'; /** file name content **/
        force_download($name,$value); /** download the file **/
   }
   
   function getTransferPdf(){
        /** variabel to post **/    
        $day01  =   $this->session->userdata('trf_day');
        $day02  =   $this->session->userdata('trf_day2');
        $acc    =   $this->session->userdata('trf_acc');
		$this->ws->set('transfers',$this->transfers->getAllTransfer($day01,$day02,$acc)); /** table transfer & pdo_transfer **/
        $this->ws->set_theme('smooth-report'); /** set theme to smooth report **/
		$data = $this->ws->view('transfer/pdf','',TRUE); /** view layout to views/transfer/pdf */
        pdf_create($data,'Rekap-Transfer'); /** call a function to create a pdf **/
   }
   
   function getTransferEmail(){
        /** variabel to post **/    
        $day01  =   $this->session->userdata('trf_day');
        $day02  =   $this->session->userdata('trf_day2');
        $acc    =   $this->session->userdata('trf_acc');
        
        /** email configuration **/
        $mail_config    =   $this->config->item('mail');
        $smtp_host      =   $mail_config['smtp_host'];
        $smtp_port      =   $mail_config['smtp_port'];
        $from_mail      =   $mail_config['from_mail'];
        $from_name      =   $mail_config['from_name'];
        
        /** php ini set email **/
        ini_set("SMTP",$smtp_host);
        ini_set("smtp_port",$smtp_port);
        ini_set('sendmail_from',$from_mail);
        /** Properties of export **/
        $subject        = 'Rekap Transfer No Rekening.'.$acc." Periode ". indonesian_date($day01) .' s/d '.indonesian_date($day02);
        $contents       = 'Dilampirkan File Rekap Transfer Dengan No Rekening '.$acc." Periode ". indonesian_date($day01) .' s/d '.indonesian_date($day02);
        $this->email->initialize($mail_config);
        $this->email->from($from_mail,$from_name);
        $email  =   'hendarsyahss@gmail.com';
        $this->email->to($email); /** email to */
        $this->email->subject($subject); /** email subject */
        $this->email->message($contents); /** email content */
        $excel  =   $this->transfers->getExcel($day01,$day02,$acc); /** get file  from excel download */
        $this->email->attach($excel);  /** email attachement */
        $d=$this->email->send(); /** email send */
        if(!$d):
            $this->session->set_flashdata('error','error');
        else:
			$this->session->set_flashdata('success','success');
        endif;
        redirect('cashflow/recap/transfer',301);
    }
     
   function getTransferReset(){
        /** post to variabel session **/
        $value  =   array('trf_day'  =>  'trf_day',
                          'trf_day2' =>  'trf_day2',
                          'trf_acc'  =>  'trf_acc');
        $this->session->unset_userdata($value); /** reset session array to userdata **/
        redirect('cashflow/recap/transfer',301); /** redirect to function transfer **/
    }
        
    
}